const { extend, Toast, Tab } = require('../../zanui/index')
import { HOST } from '../../config/apiConfig.js'

//获取应用实例
const app = getApp()

Page(extend({}, Tab, Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    isShowNoDataStatus: false,
    belongHotelId: '',
    hotelRoomData: [],
    msg: '暂无数据',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '房型列表'
    })
    const _self = this;
    if (options.id != '' && options.id != undefined) {
      _self.setData({
        belongHotelId: options.id
      });
      wx.showLoading({
        title: '正在加载中',
      });

      wx.request({
        url: 'https://qdndc.ideatower.cn/v1/hotelRoom?belongHotelId=' + _self.data.belongHotelId,
        success: (res) => {
          if (res.data.data.length > 0) {
            _self.setData({
              hotelRoomData: res.data.data
            });
            wx.hideLoading();
          } else {
            _self.setData({
              isShowNoDataStatus: true,
              msg: '暂无数据'
            });
            wx.hideLoading();
            return
          }
        }, 
        fail: (err) => {
          _self.setData({
            isShowNoDataStatus: true,
            msg: '获取失败'
          });
          wx.hideLoading();
          return
        }
      })
    } else {
      wx.showToast({
        title: '获取所属酒店ID失败',
        icon: 'none',
        duration: 2000
      })
    }
  },

  loadPageData: function () {
    const _self = this
    wx.showLoading({
      title: '正在加载中',
    });

    if (_self.data.belongHotelId == '' || _self.data.belongHotelId == null || _self.data.belongHotelId == undefined) {
      wx.showToast({
        title: '获取酒店ID失败,请返回上级界面',
        icon: 'none',
        duration: 2000
      })
      return
    }

    wx.request({
      url: 'https://qdndc.ideatower.cn/v1/hotelRoom?belongHotelId=' + _self.data.belongHotelId,
      success: (res) => {
        if (res.data.data.length > 0) {
          _self.setData({
            hotelRoomData: res.data.data
          });
          wx.hideLoading();
        } else {
          _self.setData({
            isShowNoDataStatus: true,
            msg: '暂无数据'
          });
          wx.hideLoading();
          return
        }
      },
      fail: (err) => {
        _self.setData({
          isShowNoDataStatus: true,
          msg: '获取失败'
        });
        wx.hideLoading();
        return
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadPageData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('触发下拉刷新')
    const _self = this;
    wx.showLoading({
      title: '正在加载中',
    });
    wx.request({
      url: 'https://qdndc.ideatower.cn/v1/hotelRoom?belongHotelId=' + _self.data.belongHotelId,
      success: (res) => {
        if (res.data.data.length > 0) {
          _self.setData({
            hotelRoomData: res.data.data
          });
          wx.hideLoading();
        } else {
          _self.setData({
            isShowNoDataStatus: true,
            msg: '暂无数据'
          });
          wx.hideLoading();
          return
        }
      },
      fail: (err) => {
        _self.setData({
          isShowNoDataStatus: true,
          msg: '获取失败'
        });
        wx.hideLoading();
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 下拉刷新
   */
  refesh: function () {
    //console.log('下拉刷新')

  },

  /**
   * 加载更多
   */
  loadMore: function () {
    //console.log('加载更多')
  },

/**
 * item点击
 */
  showHotelRoomDetail: function (e) {
    //console.log(this.data.hotelRoomData[e.currentTarget.id])
    //导航到编辑信息jiemian
    wx.navigateTo({
      url: '../hotelRoom/hotelRoom?data=' + JSON.stringify(this.data.hotelRoomData[e.currentTarget.id]),
    });
  }

}))